package com.iss.umservice.controller;

import java.io.PrintWriter;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.iss.iframework.security.OperatorUserEntry;
import com.iss.iframework.web.ResultCall;
import com.iss.iframework.web.support.tld.pagination.Navigation;
import com.iss.iframework.web.support.tld.pagination.PageUtil;
import com.iss.iframework.web.support.tld.pagination.Pagination;
import com.iss.umservice.bean.AnnouncementBean;
import com.iss.umservice.bean.AnnouncementItemBean;
import com.iss.umservice.bean.AnnouncementNoReadQueryBean;
import com.iss.umservice.bean.AnnouncementReadBean;
import com.iss.umservice.bean.AnnouncementReadedQueryBean;
import com.iss.umservice.bean.AnnouncementReadedResultBean;
import com.iss.umservice.bean.AnnouncementReadedResultSummaryBean;
import com.iss.umservice.bean.QueryAnnouncementBean;
import com.iss.umservice.common.SqlDatePropertyEditor;
import com.iss.umservice.manager.AnnouncementManager;

/**
 * 通知公告相关
 * 
 * @author luzi
 * 
 */
@Controller
@RequestMapping("/announcement/*")
@SessionAttributes("curUser")
public class AnnouncementController {
	@Autowired
	private AnnouncementManager announcementManager;

	@RequestMapping
	public String queryReadedAnnResultSummary(AnnouncementReadedResultSummaryBean bean,Map<String, Object> model){
		List<AnnouncementReadedResultSummaryBean> annList = announcementManager
				.queryReadedAnnResultSummary(bean);

		model.put("annList", annList);
		model.put("bean", bean);

		return "announcement/readedAnnResultSummary";
	}
	
	
	@RequestMapping
	public String queryReadedAnnResult(AnnouncementReadedResultBean bean, String pages,
			Map<String, Object> model){
		int count = announcementManager.countReadedAnnResult(bean);
		
		Pagination pagination = PageUtil.createPage(pages, count,
				"../announcement/queryReadedAnnResult.html");

		List<AnnouncementReadedResultBean> annList = announcementManager
				.queryReadedAnnResult(bean, pagination.getOffset(),
						pagination.getRowSize());

		model.put("annList", annList);
		model.put(Navigation.PAGINATION, pagination);

		return "announcement/readedAnnResult";
	}
	
	@RequestMapping
	public void readedAnn(int annId,String receiptMsg,@ModelAttribute("curUser") OperatorUserEntry userEntry, Integer receiptSelectionId,HttpServletResponse response) {
		String currUserId = userEntry.extendsInfo().get("user.accountCode").get(0);
		announcementManager.readedAnn(annId, currUserId, receiptMsg, receiptSelectionId);
		PrintWriter writer = null;
		try {
			response.setCharacterEncoding("UTF-8");
			writer = response.getWriter();
			writer.write("OK");
		} catch (Exception e) {
		}
	}
	
	@RequestMapping
	public String readAnn(int annId, Map<String, Object> model) {
		AnnouncementBean ann = new AnnouncementBean();
		ann.setId(annId);
		ann = announcementManager.getAnnouncement(ann);
		model.put("ann", ann);
		return "announcement/readAnn";
	}
	@RequestMapping
	public String read2Ann(int annId,@ModelAttribute("curUser") OperatorUserEntry userEntry, Map<String, Object> model) {
		String currUserId = userEntry.extendsInfo().get("user.accountCode").get(0);
		AnnouncementBean ann = new AnnouncementBean();
		ann.setId(annId);
		ann = announcementManager.getAnnouncement(ann);
		AnnouncementReadBean rann = announcementManager.getReadedAnn(annId, currUserId);
		model.put("ann", ann);
		model.put("readedAnn", rann);
		return "announcement/read2Ann";
	}

	@RequestMapping
	public String queryNoReadAnnouncementList(
			@ModelAttribute("curUser") OperatorUserEntry userEntry,
			AnnouncementNoReadQueryBean ann, String pages,
			Map<String, Object> model) {
		String currUserId = userEntry.extendsInfo().get("user.accountCode").get(0);

		int count = announcementManager.countNoReadAnn(currUserId);

		Pagination pagination = PageUtil.createPage(pages, count,
				"../announcement/queryNoReadAnnouncementList.html");

		List<AnnouncementNoReadQueryBean> annList = announcementManager
				.queryNoReadAnn(currUserId, pagination.getOffset(),
						pagination.getRowSize());

		model.put("annList", annList);
		model.put(Navigation.PAGINATION, pagination);

		return "announcement/lookAnnouncement";
	}

	@RequestMapping
	public String queryReadedAnnouncementList(
			@ModelAttribute("curUser") OperatorUserEntry userEntry,
			AnnouncementReadedQueryBean ann, String pages,
			Map<String, Object> model) {
		String currUserId = userEntry.extendsInfo().get("user.accountCode").get(0);

		int count = announcementManager.countReadedAnn(currUserId);

		Pagination pagination = PageUtil.createPage(pages, count,
				"../announcement/queryReadedAnnouncementList.html");

		List<AnnouncementReadedQueryBean> annList = announcementManager
				.queryReadedAnn(currUserId, pagination.getOffset(),
						pagination.getRowSize());

		model.put("annList", annList);
		model.put(Navigation.PAGINATION, pagination);

		return "announcement/lookedAnnouncement";
	}

	@RequestMapping
	public String queryAnnouncementList(
			@ModelAttribute("curUser") OperatorUserEntry userEntry,
			QueryAnnouncementBean ann, String pages, Map<String, Object> model) {
		int count = announcementManager.countAnnouncement(ann);

		Pagination pagination = PageUtil.createPage(pages, count,
				"../announcement/queryAnnouncementList.html");

		List<QueryAnnouncementBean> annList = announcementManager
				.queryAnnouncements(ann, pagination.getOffset(),
						pagination.getRowSize());

		// pagination.addCondition("companyName", company.getCompanyname());
		// pagination.addCondition("companyStatus", company.getCompanystatus());

		model.put("announcement", ann);
		model.put("announcementList", annList);
		model.put(Navigation.PAGINATION, pagination);

		return "announcement/announcementList";
	}

	@RequestMapping
	public void delAnnouncements(int[] deleteIds) {
		announcementManager.discardAnnouncement(deleteIds);
		ResultCall.makeSuccess("../announcement/queryAnnouncementList.html");
	}

	/**
	 * 跳转界面
	 * 
	 * @return
	 */
	@RequestMapping
	public void insertAnnouncementInfo(Map<String, Object> model) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		model.put("now", fmt.format(new java.util.Date()));
	}

	@RequestMapping
	public void addAnnouncementInfo(AnnouncementBean newAnn,@ModelAttribute("curUser") OperatorUserEntry userEntry,
			HttpServletRequest request) {
		String currUserId = userEntry.extendsInfo().get("user.accountCode").get(0);
		newAnn.setCreateby(currUserId);
		newAnn = bindItems(newAnn, request);
		announcementManager.addAnnouncement(newAnn);
		ResultCall.makeSuccess("../announcement/queryAnnouncementList.html");
	}
	
	@RequestMapping
	public String gotoAnnouncementInfo(int id, Map<String, Object> model) {
		AnnouncementBean ann = new AnnouncementBean();
		ann.setId(id);
		ann = announcementManager.getAnnouncement(ann);
		model.put("announcement", ann);
		return "announcement/announcementInfo";
	}

	@RequestMapping
	public String gotoModAnnouncementInfo(int id, Map<String, Object> model,
			HttpSession session) {
		session.setAttribute("willModAnnId", id);
		AnnouncementBean ann = new AnnouncementBean();
		ann.setId(id);
		ann = announcementManager.getAnnouncement(ann);
		model.put("announcement", ann);
		return "announcement/updateAnnouncementInfo";
	}

	@RequestMapping
	public void modAnnouncementInfo(AnnouncementBean ann, HttpSession session,
			HttpServletRequest request) {
		ann = bindItems(ann, request);
		ann.setId((Integer) session.getAttribute("willModAnnId"));
		announcementManager.modAnnouncement(ann);
		ResultCall.makeSuccess("../announcement/queryAnnouncementList.html");
	}

	@InitBinder
	protected void initBinder(WebDataBinder binder) throws Exception {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		SqlDatePropertyEditor dateEditor = new SqlDatePropertyEditor(fmt, true);
		binder.registerCustomEditor(Date.class, dateEditor);
	}

	private AnnouncementBean bindItems(AnnouncementBean ann,
			HttpServletRequest request) {
		String[] titles = request.getParameterValues("receiptItems_title");
		String[] descs = request.getParameterValues("receiptItems_description");
		ann.setReceiptItems(new ArrayList());
		if (titles != null)
			for (int i = 0; i < titles.length; i++) {
				AnnouncementItemBean t = new AnnouncementItemBean();
				t.setTitle(titles[i]);
				t.setDescription(descs[i]);
				ann.getReceiptItems().add(t);
			}
		return ann;
	}
}